Skip to main content

Java final, finally and finalize

Banner java icon

πŸŽ‰ Java Fun: final, finally, and finalize Explained! πŸš€β€‹

Welcome to this exciting Java tutorial where we unravel the mysteries of final, finally, and finalize. Buckle up! πŸš€


πŸ† 1. Java final Keyword​

The final keyword is like a "No Entry" sign for modifications. It can be applied to variables, methods, and classes, but it behaves differently in each case. Let’s explore! πŸ”

πŸ”Ή 1.1. final Variables – Once Set, Forever Unchanged​

A variable marked final can be assigned a value only once! Here’s where you can initialize it:

βœ” At the time of declaration πŸ—οΈ
βœ” Inside the constructor 🏠

Code Time! ⏳​

public class MyClass {
// During declaration
public final String MAJOR_VERSION = "1";
public final String MINOR_VERSION;

public MyClass() {
// In constructor
this.MINOR_VERSION = "2";
}
}

πŸ’‘ Fact: Once initialized, trying to change a final variable will lead to a compilation error 🚨:

"The final field MyClass.VERSION cannot be assigned."


πŸ”Ή 1.2. final Methods – No Override Allowed! πŸš«β€‹

A method marked final is like a parent who says, "No, you can't change the family rules!" 🎭
It cannot be overridden by child classes.

Code Time0! ⏳​

public class ParentClass {
public final void showMyName() {
System.out.println("In ParentClass");
}
}

❌ Trying to override this method? Say hello to a compile-time error:

"Cannot override the final method from ParentClass."


πŸ”Ή 1.3. final Classes – The End of Inheritance! ⛔​

A final class is off-limits for inheritance. No subclasses allowed! 🚷

Code Time1! ⏳​

public final class ParentClass {
public void showMyName() {
System.out.println("In ParentClass");
}
}

πŸ‘€ Trying to inherit from this class?

🚨 ERROR: "Cannot inherit from final ParentClass."


πŸ’₯ 2. Java finally Block – Cleanup Crew in Action! πŸ§Ήβ€‹

The finally block is like that one friend who ALWAYS shows up, no matter what. Whether an exception occurs or not, this block will execute.

πŸ›  Use Case: Releasing Resources​

βœ” Closing files πŸ“‚
βœ” Closing database connections πŸ—„οΈ
βœ” Releasing memory 🧠

Code Time2! ⏳​

try {
// Open file πŸ“
// Read file πŸ“–
}
catch(Exception e) {
// Handle exception 🚨
}
finally {
// Close file πŸ”’
}

πŸ’‘ Fact: The finally block is like a janitorβ€”it always cleans up before leaving the room! 🧽


πŸ—‘οΈ 3. Java finalize() Method – Garbage Collector’s Goodbye! πŸ‘‹β€‹

⚠️ Note: Java 18 (JEP-421) has deprecated finalize(), and it will be removed in a future release! 😱

finalize() is a special method that the garbage collector calls when an object is about to be trashed! πŸ—‘οΈ

πŸ”Ή Defined in Object Class​

protected void finalize() throws Throwable

πŸš€ Key Facts:​

βœ” Used for cleanup before the object is destroyed. 🧹
βœ” If an exception is thrown in finalize(), it's ignored. 🚫
βœ” finalize() is never called more than once for the same object. πŸ”„βŒ

πŸ“’ Pro Tip: Instead of finalize(), use try-with-resources or cleaners for resource management. 😎


🎯 4. Conclusion – The Final Showdown! πŸŽ¬β€‹

KeywordPurpose 🎯Can Be Used With? πŸ€”Key Rule πŸ“œ
finalRestriction πŸ›‘Variables, Methods, ClassesNo changes allowed! ❌
finallyCleanup 🧹Try-Catch BlocksAlways executes! πŸ”„
finalize()Cleanup Before GC πŸ—‘οΈObjectsDeprecated! 🚨

πŸ’‘ Fun Fact: The only thing final, finally, and finalize() have in common is… they all start with β€˜final’! 🀯


πŸ”₯ That’s it, folks! You’re now a pro at final, finally, and finalize()! Keep coding and stay awesome! πŸ’»βœ¨

Happy Learning! πŸš€πŸ˜ƒ